package spring.security.core.util;

import java.io.Serializable;
import java.util.List;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

public class StringUtils extends org.apache.commons.lang3.StringUtils {

	public static String join(String separator, String[] parts) {
		return Joiner.on(separator).join(parts);
	}

	public static String join(String separator, List<String> parts) {
		return Joiner.on(separator).join(parts);
	}

	public static List<String> on(String separator, String sequence) {
		return Splitter.on(separator).splitToList(sequence);
	}

	public static List<String> on(String separator, String sequence, boolean trimEmpty) {
		return trimEmpty ? Splitter.on(separator).trimResults().omitEmptyStrings().splitToList(sequence)
				: Splitter.on(separator).splitToList(sequence);
	}

	public static boolean contains(String sourceStr, Object targetObj, String pad) {
		return padIfNeed(sourceStr, pad).contains(padIfNeed(targetObj.toString(), pad));
	}

	public static <T extends Number> boolean contains(String sourceStr, T targetStr, String pad) {
		return padIfNeed(sourceStr, pad).contains(join(new Serializable[] { pad, targetStr, pad }));
	}

	private static String padIfNeed(String str, String pad) {
		StringBuilder sb = new StringBuilder(str.length() + pad.length() * 2);
		if (!str.startsWith(pad)) {
			sb.append(pad);
		}

		sb.append(str);
		if (!str.endsWith(pad)) {
			sb.append(pad);
		}

		return sb.toString();
	}
	
}
